<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
        integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <!-- 可选的 Bootstrap 主题文件（一般不用引入） -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css"
        integrity="sha384-6pzBo3FDv/PJ8r2KRkGHifhEocL+1X2rVCTTkUfGk7/0pbek5mMa1upzvWbrUbOZ" crossorigin="anonymous">
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
        integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous">
    </script>

            <style>
                h1 {
                    text-align: center;
                }
                
                table {
                    margin: 0 auto;
                    width: 60%;
                    border: 2px solid #aaa;
                    border-collapse: collapse;
                }
                
                table th, table td {
                    border: 2px solid #aaa;
                    padding: 5px;
                }
                
                th {
                    background-color: #eee;
                }
                </style>
                
</head>
<body>
    <h1>真划算</h1>
    <table id="tb1">
        <tr>
            <th>商品</th>
            <th>单价(元)</th>
            <th>颜色</th>
            <th>库存</th>
            <th>好评率</th>
            <th>操作</th>
        </tr>
        <tr>
            <td>罗技M185鼠标</td>
            <td>80</td>
            <td>黑色</td>   

            <td>892</td>
            <td>96%</td>
            <td align="center"><input type="button" value="加入购物车"
                onclick="add_shoppingcart(this);"/></td>
        </tr>
        <tr>
            <td>微软X470键盘</td>
            <td>150</td>
            <td>黑色</td>
            <td>9027</td>
            <td>96%</td>
            <td align="center"><input type="button" value="加入购物车"
                onclick="add_shoppingcart(this);"/></td>
        </tr>
        <tr>
            <td>洛克iphone6手机壳</td>+           <td>60</td>
            <td>透明</td>
            <td>671</td>
            <td>99%</td>
            <td align="center"><input type="button" value="加入购物车"
                onclick="add_shoppingcart(this);"/></td>
        </tr>
        <tr>
            <td>蓝牙耳机</td>
            <td>100</td>
            <td>蓝色</td>
            <td>8936</td>
            <td>95%</td>
            <td align="center"><input type="button" value="加入购物车"
                onclick="add_shoppingcart(this);"/></td>
        </tr>
        <tr>
            <td>金士顿U盘</td>
            <td>70</td>
            <td>红色</td>
            <td>481</td>
            <td>100%</td>
            <td align="center"><input type="button" value="加入购物车"
                onclick="add_shoppingcart(this);"/></td>
        </tr>
    </table>

    <h1>购物车</h1>
    <table>
        <thead>
            <tr>
                <th>商品</th>
                <th>单价(元)</th>
                <th>数量</th>
                <th>金额(元)</th>
                <th>删除</th>
            </tr>

            <tr>
                
            </tr>
        </thead>
        <tbody id="goods">
 
        </tbody>
        <tfoot>
            <tr>
                <td colspan="3" align="right">总计</td>
                <td id="total"></td>
                <td></td>
            </tr>
        </tfoot>
    </table>
</body>
<script>
                function add_shoppingcart(btn) {
                    var name = $(btn).parent().siblings().eq(0).html();
                    var price = $(btn).parent().siblings().eq(1).html();
                    var $tr = $('<tr>'
                            + '<td>'
                            + name
                            + '</td>'
                            + '<td>'
                            + price
                            + '</td>' 
                            + '<td align="center">'
                            + '<input type="button" value="-" onclick="decrease(this);"/>'
                            + '<input type="text" size="3" readonly value="1"/>'
                            + '<input type="button" value="+" onclick="increase(this);"/>'
                            + '</td>'
                            + '<td>'
                            + price
                            + '</td>'
                            + '<td align="center"><input type="button" value="x" onclick="del(this);"/></td>'
                            + '</tr>');  
                    // 将此行tr追加到tbody下
                    $("#goods").append($tr);
                    sum();
                }
                   // 从购物车中删除物品
                function del(btn) {
                    $(btn).parent().parent().remove();
                    sum();
                }; Z

                // 购物车中实现点击按钮增加数量
                function increase(btn) {
                    var amount = $(btn).prev().val();
                    $(btn).prev().val(++amount);
                    var price = $(btn).parent().prev().html();
                    var mny = price * amount;
                    $(btn).parent().next().html(mny);
                    sum();
                };.0

                // 减法
                function decrease(btn) {
                   
                    var amount = $(btn).next().val();
                    if (amount <= 1) {
                        return;
                    }
                    
                    $(btn).next().val(--amount);
                    
                    var price = $(btn).parent().prev().html();
                    
                    var mny = price * amount;
                   
                    $(btn).parent().next().html(mny);
                    sum();
                };
                // 求订单的总计
                function sum() {
                   
                    var $trs = $("#goods tr");
                   
                    var sum = 0;
                    for (var i = 0; i < $trs.length; i++) {
                       
                        var $tr = $trs.eq(i);
                       
                        var mny = $tr.children().eq(3).html();
                        sum += parseFloat(mny);
                    }

                    $("#total").html(sum);
                };   




                
</script>
</html>